package com.fastbee.sip.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fastbee.sip.domain.MediaServer;
import com.fastbee.sip.domain.SipDevice;
import com.fastbee.sip.domain.SipDeviceChannel;
import com.fastbee.sip.mapper.SipDeviceChannelMapper;
import com.fastbee.sip.model.Stream;
import com.fastbee.sip.server.IRtspCmd;
import com.fastbee.sip.server.ISipCmd;
import com.fastbee.sip.service.*;
import com.fastbee.sip.util.ZlmApiUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class PlayServiceImpl implements IPlayService {

    @Autowired
    private ISipCmd sipCmd;

    @Autowired
    private IRtspCmd rtspCmd;

    @Autowired
    private IZmlHookService zmlHookService;

    @Autowired
    private ISipCacheService sipCacheService;

    @Autowired
    private ISipDeviceService sipDeviceService;

    @Autowired
    private IMediaServerService mediaServerService;

    @Autowired
    private SipDeviceChannelMapper sipDeviceChannelMapper;

    @Autowired
    private ZlmApiUtils zlmApiUtils;

    @Override
    public Stream play(String deviceId, String channelId, boolean record) {
        SipDevice dev = sipDeviceService.selectSipDeviceBySipId(deviceId);
        Stream streaminfo = sipCacheService.queryPlayByDevice(deviceId, channelId,record);
        SipDeviceChannel channel = new SipDeviceChannel();
        channel.setChannelSipId(channelId);
        if (record) {
            channel.setStreamid(String.format("gb_playrecord_%s_%s", dev.getDeviceSipId(), channelId));
        } else {
            channel.setStreamid(String.format("gb_play_%s_%s", dev.getDeviceSipId(), channelId));
        }
        if (streaminfo == null) {
            sipCmd.playStreamCmd(dev, channelId, record);
            sipDeviceChannelMapper.updateChannelStreamId(channel);
            Stream info = new Stream(dev.getDeviceSipId(), channelId, channel.getStreamid());
            return zmlHookService.updateStream(info);
        } else {
            MediaServer mediaInfo = mediaServerService.selectMediaServerBydeviceSipId(deviceId);
            JSONObject rtpInfo = zlmApiUtils.getRtpInfo(mediaInfo, channel.getStreamid());
            if (!rtpInfo.getBoolean("exist") || record) {
                sipCmd.playStreamCmd(dev, channelId, record);
                sipDeviceChannelMapper.updateChannelStreamId(channel);
            }
            log.info("getRtpInfo： {}", rtpInfo);
            return zmlHookService.updateStream(streaminfo);
        }
    }

    @Override
    public String closeStream(String deviceId, String streamId) {
        MediaServer mediaInfo = mediaServerService.selectMediaServerBydeviceSipId(deviceId);
        JSONObject ret = zlmApiUtils.getMediaList(mediaInfo, "rtp", "rtmp", streamId);
        int code = ret.getInteger("code");
        if(code == 0) {
            int totalReaderCount = ret.getInteger("totalReaderCount");
            log.info("还有{}位用户正在观看该流！", totalReaderCount);
            if (totalReaderCount == 0) {
                sipCmd.streamByeCmd(deviceId, streamId);
            }
        } else {
            log.info("流详细信息：{}，错误码：{}", ret, code);
        }
        return "";
    }

    @Override
    public Stream playback(String deviceId, String channelId, String startTime, String endTime) {
        SipDevice dev = sipDeviceService.selectSipDeviceBySipId(deviceId);
        String ssrc = sipCmd.playbackStreamCmd(dev, channelId, startTime, endTime);
        Stream streaminfo = new Stream(dev.getDeviceSipId(), channelId, ssrc);
        return zmlHookService.updateStream(streaminfo);
    }

    @Override
    public String playbackPause(String deviceId, String channelId, String streamId) {
        SipDevice dev = sipDeviceService.selectSipDeviceBySipId(deviceId);
        Stream streamInfo = sipCacheService.queryPlaybackByStreamId(streamId);
        if (null == streamInfo) {
            return "streamId不存在";
        }
        rtspCmd.setCseq(streamInfo.getStreamId());
        rtspCmd.playPause(dev, channelId, streamInfo.getStreamId());
        return null;
    }

    @Override
    public String playbackReplay(String deviceId, String channelId, String streamId) {
        SipDevice dev = sipDeviceService.selectSipDeviceBySipId(deviceId);
        Stream streamInfo = sipCacheService.queryPlaybackByStreamId(streamId);
        if (null == streamInfo) {
            return "streamId不存在";
        }
        rtspCmd.setCseq(streamInfo.getStreamId());
        rtspCmd.playReplay(dev, channelId, streamInfo.getStreamId());
        return null;
    }

    @Override
    public String playbackSeek(String deviceId, String channelId, String streamId, long seektime) {
        SipDevice dev = sipDeviceService.selectSipDeviceBySipId(deviceId);
        Stream streamInfo = sipCacheService.queryPlaybackByStreamId(streamId);
        if (null == streamInfo) {
            return "streamId不存在";
        }
        rtspCmd.setCseq(streamInfo.getStreamId());
        rtspCmd.playBackSeek(dev, channelId, streamInfo.getStreamId(), seektime);
        return null;
    }

    @Override
    public String playbackSpeed(String deviceId, String channelId, String streamId, Integer speed) {
        SipDevice dev = sipDeviceService.selectSipDeviceBySipId(deviceId);
        Stream streamInfo = sipCacheService.queryPlaybackByStreamId(streamId);
        if (null == streamInfo) {
            return "streamId不存在";
        }
        rtspCmd.setCseq(streamInfo.getStreamId());
        rtspCmd.playBackSpeed(dev, channelId, streamInfo.getStreamId(), speed);
        return null;
    }
}
